package com.springmvc.api.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created by cheng.xi on 12/10/15.
 */
@Aspect
@Component("testAspectAnnotation")
public class TestAspectAnnotation {

    Logger LOGGER = Logger.getLogger(TestAspectAnnotation.class);

    @Pointcut("execution(* com.springmvc.api.aop.*.*(..))")
    private void cutMethod(){}


    @After("cutMethod()")
    public void doAfter(JoinPoint jp){
        LOGGER.debug("日志结束方法:"
                + jp.getTarget().getClass().getName()
                + "."
                + jp.getSignature().getName());
    }

    @Around("cutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
        long time = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        time = System.currentTimeMillis() - time;
        LOGGER.debug("处理时间:" + time + "ms");
        return retVal;
    }

    @Before("cutMethod()")
    public void doBefore(JoinPoint jp){
        LOGGER.debug("日志开始方法:"
                + jp.getTarget().getClass().getName()
                + "."
                + jp.getSignature().getName());
    }

    @AfterThrowing(value = "cutMethod()",throwing = "ex")
    public void doThrowing(JoinPoint jp,Throwable ex){
        LOGGER.debug("方法:"
                + jp.getTarget().getClass().getName()
                + "."
                + jp.getSignature().getName() + "抛出异常");
        LOGGER.debug(ex.getMessage());
    }
}
